home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
src
/
fig03_10.jar
/
Ch03
/
Fig03_10
/
Fig03_10.cpp
Wrap
C/C++ Source or Header
|
1997-10-11
|
1KB
|
66 lines
// Fig. 3.10: fig03_10.cpp
// Craps
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int rollDice( void ); // function prototype
int main()
{
enum Status { CONTINUE, WON, LOST };
int sum, myPoint;
Status gameStatus;
srand( time( NULL ) );
sum = rollDice(); // first roll of the dice
switch ( sum ) {
case 7:
case 11: // win on first roll
gameStatus = WON;
break;
case 2:
case 3:
case 12: // lose on first roll
gameStatus = LOST;
break;
default: // remember point
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is " << myPoint << endl;
break; // optional
}
while ( gameStatus == CONTINUE ) { // keep rolling
sum = rollDice();
if ( sum == myPoint ) // win by making point
gameStatus = WON;
else
if ( sum == 7 ) // lose by rolling 7
gameStatus = LOST;
}
if ( gameStatus == WON )
cout << "Player wins" << endl;
else
cout << "Player loses" << endl;
return 0;
}
int rollDice( void )
{
int die1, die2, workSum;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
workSum = die1 + die2;
cout << "Player rolled " << die1 << " + " << die2
<< " = " << workSum << endl;
return workSum;
}